<template>
    <a-modal
        :title="!currRow ? '添加疑难解答' : '疑难解答'"
        :visible="isShowModal"
        width="40%"
        :footer="null"
        dialogClass="custom-modal"
        @cancel="handleCancel"
        :maskClosable="false"
    >
        <a-form-model
            :model="form"
            ref="form"
            :labelCol="{ span: 5 }"
            :wrapperCol="{ span: 19 }"
            :rules="rules"
            :colon="false"
        >
            <a-row :gutter="10">
                <a-col :span="24" class="mb16">
                    <a-form-model-item label="疑问" prop="title">
                        <a-input placeholder="请输入问题" v-model.trim="form.title" allowClear />
                    </a-form-model-item>
                </a-col>
                <a-col :span="24" class="mb16">
                    <a-form-model-item label="解答" prop="content">
                        <a-textarea
                            placeholder="请输入解答"
                            v-model.trim="form.content"
                            allowClear
                            :auto-size="{ minRows: 3, maxRows: 5 }"
                        />
                    </a-form-model-item>
                </a-col>
                <a-col :span="24">
                    <a-button type="primary" class="fr" @click="handleOk" :loading="confirmLoading">确定</a-button>
                </a-col>
            </a-row>
        </a-form-model>
    </a-modal>
</template>
<script>
import { qaRestApi } from '../../../common/api/setting'

export default {
    data() {
        return {
            confirmLoading: false,
            form: {
                title: '',
                content: '',
            },
            rules: {
                title: [{ required: true, message: '请输入疑问', trigger: 'blur' }],
                content: [{ required: true, message: '请输入解答', trigger: 'blur' }],
            },
        }
    },
    components: {},
    props: {
        isShowModal: {
            required: true,
            type: Boolean,
        },
        currRow: {
            required: false,
            type: [Object, undefined],
        },
        enterpriseId: {
            required: true,
            type: String,
        },
    },
    watch: {
        isShowModal: {
            immediate: true,
            handler: function (isShowModal) {
                if (isShowModal) {
                    this.beforeShowModal()
                } else {
                    this.$refs.form && this.$refs.form.resetFields()
                }
            },
        },
    },
    beforeDestroy() {
        this.$refs.form && this.$refs.form.resetFields()
    },
    methods: {
        beforeShowModal() {
            if (this.currRow) {
                const { content, title } = this.currRow
                Object.assign(this.form, {
                    content,
                    title,
                })
            }
        },
        async save() {
            const apiType = this.currRow ? 'edit' : 'add'
            const { content, title } = this.form
            var ps = {
                content,
                title,
            }

            if (this.currRow) {
                ps['id'] = this.currRow.id
            }

            await qaRestApi[apiType](ps)
        },
        async handleOk() {
            this.confirmLoading = true
            this.$refs.form.validate(async valid => {
                console.log(valid, 'valid')
                if (valid) {
                    this.save()
                        .then(() => {
                            this.$emit('update:isShowModal', false)
                            this.$message.success('操作成功')
                            this.$emit('updateData')
                        })
                        .catch(e => {
                            console.log(e)
                            this.$message.error(e.message)
                        })
                        .finally(() => {
                            this.confirmLoading = false
                        })
                } else {
                    this.confirmLoading = false
                    return false
                }
            })
        },
        handleCancel() {
            this.$emit('update:isShowModal', false)
        },
    },
}
</script>
<style scoped></style>
